<template>
  <div class="app-container">

    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary"
                 icon="el-icon-edit">添加
      </el-button>
    </div>

    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column type="index" label='#' align="center" sortable/>
      <el-table-column prop="foreignName" label="外文名" align="center" sortable/>
      <el-table-column prop="chineseName" label="中文名" align="center" sortable/>
      <el-table-column prop="gender" label="性别" align="center" sortable/>
      <el-table-column prop="birthday" label="生日" align="center" sortable/>
      <el-table-column prop="nationality" label="国籍" align="center" sortable/>
      <el-table-column prop="birthplace" label="出生地" align="center" sortable/>
      <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="listQuery.current" :page-sizes="[10,20,30,50]" :page-size="listQuery.size"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="80px"
               style='width: 400px; margin-left:70px;'>

        <el-form-item label="外文名" prop="foreignName">
          <el-input v-model="temp.foreignName"/>
        </el-form-item>
        <el-form-item label="中文名" prop="chineseName">
          <el-input v-model="temp.chineseName"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="temp.gender" label="0">男</el-radio>
          <el-radio v-model="temp.gender" label="1">女</el-radio>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker v-model="temp.birthday" type="date" placeholder="选择日期时间" value-format="yyyy-MM-dd"/>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="temp.nationality"/>
        </el-form-item>
        <el-form-item label="出生地" prop="birthplace">
          <el-input v-model="temp.birthplace"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus==='create'" type="primary" @click="createData">确认</el-button>
        <el-button v-else type="primary" @click="updateData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import {listDirectorPage, deleteDirector, createDirector, updateDirector} from "@/api/director";

  export default {
    data() {
      return {
        total: null,
        list: null,
        listLoading: true,
        listQuery: {
          current: 1,
          size: 10,
        },
        temp: {
          id: undefined,
          foreignName: '',
          chineseName: '',
          gender: '0',
          birthday: '',
          nationality: '',
          birthplace: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          foreignName: [{required: true, message: '外文名不能为空', trigger: 'blur'}],
          chineseName: [{required: true, message: '中文名不能为空', trigger: 'blur'}],
          gender: [{required: true, message: '性别不能为空', trigger: 'blur'}],
          birthday: [{required: true, message: '生日不能为空', trigger: 'blur'}]
        },
      }
    },
    filters: {},
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        this.listLoading = true
        listDirectorPage(this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        delete this.temp.createTime
        delete this.temp.updateTime
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      handleDelete(row) {
        deleteDirector(row.id).then(response => {
          this.$message({
            message: response.message,
            type: 'success'
          })
          for (const v of this.list) {
            if (v.id === row.id) {
              const index = this.list.indexOf(v)
              this.list.splice(index, 1)
              break
            }
          }
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            createDirector(this.temp).then((response) => {
              this.temp.id = response.data.directorId
              this.list.unshift(this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updateDirector(this.temp).then(() => {
              for (const v of this.list) {
                if (v.id === this.temp.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      resetTemp() {
        this.temp = {
          id: undefined,
          foreignName: '',
          chineseName: '',
          gender: '0',
          birthday: '',
          nationality: '',
          birthplace: ''
        }
      },
      handleFilter() {
        this.listQuery.current = 1
        this.fetchData()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.listQuery.current = val
        this.fetchData()
      }
    }
  }
</script>
